<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>更新页面</title>
        <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
        <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
    </head>
    <body>

        <form class="layui-form">

            <div class="mainBox">
                <div class="main-container mr-5">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">等级</label>
                        <div class="layui-input-block">
                            <div name="level" id="level"></div>
                        </div>
                    </div>

                    <div class="layui-form-item" id="area-setting" style="display: none;">
                        <label class="layui-form-label">区域设置</label>
                        <div class="layui-input-block">
                            <div name="level_province_id" id="level_province_id"></div>
                            <div name="level_city_id" id="level_city_id"></div>
                            <div name="level_district_id" id="level_district_id"></div>
                        </div>
                    </div>
                    
                </div>
            </div>

            <div class="bottom">
                <div class="button-container">
                    <button type="submit" class="pear-btn pear-btn-primary pear-btn-md" lay-submit="" lay-filter="save">
                        提交
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        重置
                    </button>
                </div>
            </div>
            
        </form>

        <script src="/app/admin/component/layui/layui.js"></script>
        <script src="/app/admin/component/pear/pear.js"></script>
        <script src="/app/admin/admin/js/permission.js"></script>
        <script>

            // 相关接口
            const PRIMARY_KEY = "id";
            const SELECT_API = "/app/admin/user/select" + location.search;
            const UPDATE_API = "/app/admin/user/update";

            var level = 0;

            // 获取数据库记录
            layui.use(["form", "util", "popup"], function () {
                let $ = layui.$;
                $.ajax({
                    url: SELECT_API,
                    dataType: "json",
                    success: function (res) {
                        
                        // 给表单初始化数据
                        layui.each(res.data[0], function (key, value) {
                            let obj = $('*[name="'+key+'"]');
                            if (key === "password") {
                                obj.attr("placeholder", "不更新密码请留空");
                                return;
                            }
                            if (typeof obj[0] === "undefined" || !obj[0].nodeName) return;
                            if (obj[0].nodeName.toLowerCase() === "textarea") {
                                obj.val(layui.util.escape(value));
                            } else {
                                obj.attr("value", value);
                            }
                        });

                        level = $('#level').attr('value');

                        if (level > 3) {
                            $('#area-setting').show();
                        } else {
                            $('#area-setting').hide();
                        }

                        layui.use(["jquery", "xmSelect"], function() {
                            layui.$.ajax({
                                url: "/app/admin/user-level/get",
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#level").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#level",
                                        name: "level",
                                        initValue: initValue,
                                        data: res.data,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                        filterable: true,
                                        on: function(data) {
                                            if (data.change.length > 0) {
                                                level = data.change[0].value;

                                                if (level > 3) {
                                                    $('#area-setting').show();

                                                    if (level == 5) {
                                                        $('#level_district_id').hide();
                                                    } else {
                                                        $('#level_district_id').show();

                                                    }

                                                } else {
                                                    $('#area-setting').hide();
                                                }


                                            }
                                        }
                                    });
                                    if (res.code) {
                                        return layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        layui.use(["jquery", "xmSelect"], function() {
                            layui.$.ajax({
                                url: "/app/admin/area/get",
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#level_province_id").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#level_province_id",
                                        prop: {
                                            name: 'area_name',
                                            value: 'id'
                                        },
                                        name: "level_province_id",
                                        initValue: initValue,
                                        data: res.data,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                        filterable: true,
                                        on: function(data) {
                                            if (data.change.length > 0) {
                                                layui.use(["jquery", "xmSelect"], function() {
                                                    layui.$.ajax({
                                                        url: "/app/admin/area/get",
                                                        data: {parent_id: data.change[0].id},
                                                        dataType: "json",
                                                        success: function (res) {
                                                            let value = layui.$("#level_city_id").attr("value");
                                                            let initValue = value ? value.split(",") : [];
                                                            layui.xmSelect.render({
                                                                el: "#level_city_id",
                                                                prop: {
                                                                    name: 'area_name',
                                                                    value: 'id'
                                                                },
                                                                name: "level_city_id",
                                                                initValue: initValue,
                                                                data: res.data,
                                                                model: {"icon":"hidden","label":{"type":"text"}},
                                                                clickClose: true,
                                                                radio: true,
                                                                filterable: true,
                                                                on: function(data) {
                                                                    if (level == 4) {
                                                                        layui.$.ajax({
                                                                            url: "/app/admin/area/get",
                                                                            data: {parent_id: data.change[0].id},
                                                                            dataType: "json",
                                                                            success: function (res) {
                                                                                let value = layui.$("#level_district_id").attr("value");
                                                                                let initValue = value ? value.split(",") : [];
                                                                                layui.xmSelect.render({
                                                                                    el: "#level_district_id",
                                                                                    prop: {
                                                                                        name: 'area_name',
                                                                                        value: 'id'
                                                                                    },
                                                                                    name: "level_district_id",
                                                                                    initValue: initValue,
                                                                                    data: res.data,
                                                                                    model: {
                                                                                        "icon": "hidden",
                                                                                        "label": {"type": "text"}
                                                                                    },
                                                                                    clickClose: true,
                                                                                    radio: true,
                                                                                    filterable: true
                                                                                });
                                                                                if (res.code) {
                                                                                    return layui.popup.failure(res.msg);
                                                                                }
                                                                            }
                                                                        });
                                                                    }
                                                                }
                                                            });
                                                            if (res.code) {
                                                                return layui.popup.failure(res.msg);
                                                            }
                                                        }
                                                    });
                                                });
                                            }
                                        }
                                    });
                                    if (res.code) {
                                        return layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        layui.use(["jquery", "xmSelect"], function() {
                            layui.$.ajax({
                                url: "/app/admin/area/get",
                                data: {parent_id: $('#level_province_id').attr('value')},
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#level_city_id").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#level_city_id",
                                        prop: {
                                            name: 'area_name',
                                            value: 'id'
                                        },
                                        name: "level_city_id",
                                        initValue: initValue,
                                        data: res.data,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                        filterable: true,
                                    });
                                    if (res.code) {
                                        return layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });

                        if (level == 4) {
                            layui.$.ajax({
                                url: "/app/admin/area/get",
                                data: {parent_id: $('#level_city_id').attr('value')},
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#level_district_id").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#level_district_id",
                                        prop: {
                                            name: 'area_name',
                                            value: 'id'
                                        },
                                        name: "level_district_id",
                                        initValue: initValue,
                                        data: res.data,
                                        model: {
                                            "icon": "hidden",
                                            "label": {"type": "text"}
                                        },
                                        clickClose: true,
                                        radio: true,
                                        filterable: true
                                    });
                                    if (res.code) {
                                        return layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        }

                        
                        // 字段 性别 sex
                        layui.use(["jquery", "xmSelect", "popup"], function() {
                            layui.$.ajax({
                                url: "/app/admin/dict/get/sex",
                                dataType: "json",
                                success: function (res) {
                                    let value = layui.$("#sex").attr("value");
                                    let initValue = value ? value.split(",") : [];
                                    layui.xmSelect.render({
                                        el: "#sex",
                                        name: "sex",
                                        initValue: initValue,
                                        data: res.data,
                                        model: {"icon":"hidden","label":{"type":"text"}},
                                        clickClose: true,
                                        radio: true,
                                    });
                                    if (res.code) {
                                        layui.popup.failure(res.msg);
                                    }
                                }
                            });
                        });
                        
                        // 字段 头像 avatar
                        layui.use(["upload", "layer"], function() {
                            let input = layui.$("#avatar").prev();
                            input.prev().attr("src", input.val());
                            layui.$("#attachment-choose-avatar").on("click", function() {
                                parent.layer.open({
                                    type: 2,
                                    title: "选择附件",
                                    content: "/app/admin/upload/attachment?ext=jpg,jpeg,png,gif,bmp",
                                    area: ["95%", "90%"],
                                    success: function (layero, index) {
                                        parent.layui.$("#layui-layer" + index).data("callback", function (data) {
                                            input.val(data.url).prev().attr("src", data.url);
                                        });
                                    }
                                });
                            });
                            layui.upload.render({
                                elem: "#avatar",
                                url: "/app/admin/upload/avatar",
                                acceptMime: "image/gif,image/jpeg,image/jpg,image/png",
                                field: "__file__",
                                done: function (res) {
                                    if (res.code > 0) return layui.layer.msg(res.msg);
                                    this.item.prev().val(res.data.url).prev().attr("src", res.data.url);
                                }
                            });
                        });
                        
                        // 字段 生日 birthday
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#birthday",
                            });
                        })
                        
                        // 字段 登录时间 last_time
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#last_time",
                                type: "datetime",
                            });
                        })
                        
                        // 字段 注册时间 join_time
                        layui.use(["laydate"], function() {
                            layui.laydate.render({
                                elem: "#join_time",
                                type: "datetime",
                            });
                        })
                        
                        // 字段 禁用 status
                        layui.use(["form"], function() {
                            layui.$("#status").attr("checked", layui.$('input[name="status"]').val() != 0);
                            layui.form.render();
                            layui.form.on("switch(status)", function(data) {
                                layui.$('input[name="status"]').val(this.checked ? 1 : 0);
                            });
                        })

                        // ajax产生错误
                        if (res.code) {
                            layui.popup.failure(res.msg);
                        }

                        layui.form.render()
                    }
                });
            });

            //提交事件
            layui.use(["form", "popup"], function () {
                layui.form.on("submit(save)", function (data) {
                    data.field[PRIMARY_KEY] = layui.url().search[PRIMARY_KEY];
                    layui.$.ajax({
                        url: UPDATE_API,
                        type: "POST",
                        dateType: "json",
                        data: data.field,
                        success: function (res) {
                            if (res.code) {
                                return layui.popup.failure(res.msg);
                            }
                            return layui.popup.success("操作成功", function () {
                                parent.refreshTable();
                                parent.layer.close(parent.layer.getFrameIndex(window.name));
                            });
                        }
                    });
                    return false;
                });
            });



        </script>

    </body>

</html>
